package com.start.common.dic;

import com.start.common.dataImport.ExcelActionType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Data
public abstract class BaseExcelMapperHandler<T> implements StartBaseDicMapperHandler<T> {
    private Map<String, String> mapperTableData = new HashMap<>();
    @Schema(name = "格式化")
    private String format;
    @Schema(name = "字段类型名称")
    private String typeName;

    @Schema(name = "excel操作类型")
    protected ExcelActionType excelActionType;

    public BaseExcelMapperHandler(ExcelActionType excelActionType) {
        this.excelActionType = excelActionType;
    }

    public abstract void loadMapperData();


    public String getDateTimeFormatOrDefault() {
        if (!StringUtils.isEmpty(format)) return format;
        return getDateTimeDefaultFormat();
    }

    public String getDateTimeDefaultFormat() {
        switch (typeName) {
            case "java.time.LocalTime":
                return "HH:mm:ss" ;
            case "java.time.LocalDate":
                return "yyyy-mm-dd" ;
            default:
                return "yyyy-MM-dd HH:mm:ss" ;
        }
    }

    @Override
    public final String getExportValue(T obj) {
        if (obj == null) throw new RuntimeException("obj  Cannot be null ");
        return analysisExportValue(obj);
    }

    public abstract String analysisExportValue(T obj);

    /**
     * 处理映射转换
     *
     * @param value
     * @return
     */
    protected String handlerMapper(String value) {
        if (value == null) return null;
        if (this.getMapperTableData() == null || this.getMapperTableData().size() < 1) return value;
        return this.getMapperTableData().getOrDefault(value, value);
    }
}
